package system.track;

/**
 * This special Block class provides special behavior for Block 8.
 *
 * This is because in the counter clockwise direction there is
 * no switch, so get next Block needs to be overridden to
 * provide Block 9 directly  Also, the getters and setters
 * for the switches need to handle this special case.
 *
 * @author David Kolb
 * @version Cycle 1 - complete
 */
public class Block8 extends GenericBlock implements Block
{
    private Block ccwBlock;

    /**
     * Constructor for Objects of class Block8.
     */
    public Block8()
    {
        super();
    }

    /**
     * The COUNTERCLOCKWISE Block will always be set to Block 9, 
     * there is no other alternative.
     * 
     * @param ccwBlock   a Block object 
     */
    public void setCCWBlock(Block ccwBlock)
    {
        this.ccwBlock = ccwBlock;
    }

    /**
     * Returns the switch in the specified direction.  Overridden to
     * automatically return a null value for Direction = COUNTERCLOCKWISE, 
     * because there is no switch in that direction.
     * 
     * @param d the direction to get the switch from.
     * @return The switch to return, null if none.
     */
    @Override
    public Switch getSwitch(Direction d)
    {
        Switch returnVal = null;
        if (d != Direction.COUNTERCLOCKWISE)
        {
            //use the default method to get the CW switch
            returnVal = super.getSwitch(d);
        }
        return returnVal;
    }

    /**
     * Registers a switch as being located at the end of the 
     * block in the direction denoted by the argument d. Overridden to
     * automatically only set a switch in the CW Direction.
     *
     * @param d the direction denoting the end that the switch is on.
     * @param s the switch located at the end of the block.
     */
    @Override
    public void setSwitch(Direction d, Switch s)
    {
        if (d != Direction.COUNTERCLOCKWISE)
        {
            //use the default method to set the CW switch
            super.setSwitch(d, s);
        }
    }

    /**
     * This gets the next block in the direction specified.
     *
     * The algorithm for getting the block takes into account the current
     * setting of the switch.  For instance, if the switch at the end is
     * oriented so there are two possible exits and is set to 180 degrees,
     * then the exit at 180 degrees is returned.
     * 
     * This method is overridden in order to only return Block 9 if the
     * Direction is COUNTERCLOCKWISE.  The usual Block algorithm is run
     * if the Direction is CW.
     * 
     * @param d the direction in which to get the next block.
     * @return the next Block
     */
    @Override
    public Block getNextBlock(Direction d)
    {
        Block returnVal = this.ccwBlock;
        if (d != Direction.COUNTERCLOCKWISE)
        {
            //use the default method to get the CW Block
            returnVal = super.getNextBlock(d);
        }
        return returnVal;
    }
}
